<?php

require_once("page.class.php");
require_once("izvodi.pexim.class.php");

class izvodi extends page {
	
	var $db;
	
	var $cell_delimiter = "{!}";
	var $row_delimiter = "{!!}";
	
	function izvodi(){
		$this->db = new db_functions();
		$a = getFromInput("a");
		$mod = getFromInput("mod");
		
		switch ($a){
			default:
				echo '<iframe src="izvodi.php?a=select" style="width:1000px; height:720px; margin:0px; padding:5px; border:none;" />';
				break;
			case "select":
				$this->importIzvodi_selectXML();
				break;
			case "show":
				$this->importIzvodi_showXML();
				break;
			case "selectUgovor":
				$this->selectUgovor();
				break;
			case "selectUgovorStrict":
				$this->selectUgovorStrict();
				break;
			case "ugovorDetails":
				$this->ugovorDetails();
				break;
			case "selectKonto":
				$this->selectKonto();
				break;
			case "selectKontoStrict":
				$this->selectKontoStrict();
				break;
			case "selectTipUplate":
				$this->selectTipUplate();
				break;
			case "selectTipUplateStrict":
				$this->selectTipUplateStrict();
				break;
			case "save":
				$this->save();
				break;
			
			///////////////////////////////// UPLATE
			case "uplate":
				$this->uplateBrowse();
				break;
			case "linkUplata":
				$this->linkUplata();
				break;
			case "searchUplataUgovor":
				$this->selectUgovor("uplataAddUgovor");
				break;
			case "uplataAddUgovor":
				$this->uplataAddUgovor();
				break;
			case "uplataDelete":
				$this->uplataDelete();
				break;
			case "uplataEdit":
				$this->uplataEdit();
				break;
			case "uplataEditSave":
				$this->uplataEditSave();
				break;
		}
	}
	
	function importIzvodi_selectXML(){
		$style = $this->loadStyles();
		echo $style;
		echo $this->parseTemplate("izvodi/import_izvodi.tpl");
	}
	
	function importIzvodi_showXML(){
		$type = strtoupper(trim(readValFromInput("type")));
		$style = $this->loadStyles();
		echo $style;
		if (strlen($type)>0 && isset($_FILES["fajl"])) {
			echo '<script>var __obj = parent.document.getElementById(\'iframeU\'); __obj.style.display=\'block\'</script>';
			$ext = strtoupper(trim(getFileExtension($_FILES["fajl"]["name"])));
			
			if ($ext == $type) {
				
				$ypu = $this->getUserId();
				$name = $_FILES["fajl"]["name"];
				$tmpname = $_FILES["fajl"]["tmp_name"];
				$size = $_FILES["fajl"]["size"];
				
				//echo $tmpname.", ".$_ENV["CONTENT_LENGTH"]."<br />".$name."<br />";die();
				
				$nfn = "tmp/tmp$ypu.$type";
				move_uploaded_file($tmpname,$nfn);
				
				$import = new IzvodiPEXIM($nfn);
				
				if($import->status["code"] === false){
					$this->assign("comment_start","<!--");
					$this->assign("comment_end","--><span class=\"error\">".$import->status["message"]."</span>");
				}else{
					//$izvod = $import->getStruct(); //print_rarray($izvod);
					$izvod = $import->fetch();
					$ukupno = 0;
					foreach ($izvod->in as $k=>$v){
						
						if (strlen($v->ugovor)) {
							$komid = intval($this->db->getOne("SELECT komitent FROM ugovori WHERE broj='$v->ugovor' ORDER BY id DESC LIMIT 1"));
							if ($komid>0) {
								$v->id = $komid;
								$v->originname = $v->name;
								$v->name = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=$komid LIMIT 1");
							}else{
								$v->originname = $v->name;
							}
						}
						
						$num[] = $k+1;
						$izvods[] = $izvod->broj;
						
						$originnames[] = $v->originname;
						$names[] = $v->name;
						$komids[] = $v->id;
						$komset[] = $v->id>0?"komset":"";
						
						$zrs[] = $v->acc;
						$banks[] = $v->bankname;
						$values[] = number_format($v->value,2,",",".");
						$ukupno += (float) $v->value;
						$ugovor[] = $v->ugovor;
						$ugoid = $this->db->select2("SELECT id FROM ugovori WHERE broj='$v->ugovor'");
						if ($ugoid !== false && count($ugoid)==1) {
							$ugoids[] = $ugoid[0]["id"];
						}else{
							$ugoids[] = "";
						}
						
						$nap = $v->originname.":\n".$v->purpose;
						$napomena[] = stripslashes($nap);
					}
					
					$this->assign("numrecords",count($num));
					
					$this->assign("ukupno_f",$ukupno);
					$this->assign("ukupno",number_format($ukupno,2,",","."));
					
					$this->assign("izvod_date",$izvod->in[0]->date);
					$this->assign("num",$num);
					$this->assign("izvod",$izvods);
					$this->assign("naziv",$names);
					$this->assign("origin_name",$originnames);
					$this->assign("komid",$komids);
					$this->assign("komset",$komset);
					$this->assign("zr",$zrs);
					$this->assign("banka",$banks);
					$this->assign("iznos",$values);
					$this->assign("ugovor",$ugovor);
					$this->assign("ugovorid",$ugoids);
					$this->assign("napomena",$napomena);
					
					$this->assign("data",var_export($struct,true));
				}
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","--><span class=\"error\">Odabrali ste nedozvoljeni tip fajla(<strong>.$ext</strong>).<br />Dozvoljeni tip fajla je <strong>.$type</strong></span>");
			}
		}else{
			$this->assign("izvod",array(0=>""));
			$this->assign("num",array(0=>"1"));
		}
		$html = $this->parseTemplate("izvodi/import_izvodi_table.tpl");
		echo $html;
	}
	
	//-----------------------------------------------------------
	
	function selectUgovor($type="import"){
		$objid = getFromInput("objid");
		$objbroj = getFromInput("objbroj");
		$objkomid = getFromInput("objkomid");
		$objkomname = getFromInput("objkomname");
		$uplid = getFromInput("uplid");
		
		$k = addslashes(getFromInput("k"));
		$this->assign("search_kword",$k);
		
		$q = "SELECT id, broj, datum, komitent, iznos_bruto, tip_ugovora "
			."FROM ugovori, komitenti "
			."WHERE komitenti.komitent_id=ugovori.komitent AND ("
			."ugovori.id='$k' "
			."OR broj LIKE '%$k%' "
			."OR komitent='$k' "
			."OR komitent_name LIKE '%$k%' "
			."OR komitent_name1 LIKE '%$k%' "
			."OR komitent_ziro LIKE '%$k%') "
			."LIMIT 0,50";
		$res = $this->db->select2($q);
		$this->assign("search_length",$res!==false?count($res):"0");
		
		if ($res !== false) {
			foreach ($res as $k=>$row){
				$id = $row["id"];
				$broj = $row["broj"];
				
				$num[]	= $broj;
				
				// dugovanje
				$q = "SELECT SUM(iznos) FROM ugovori_otplate WHERE ugoid=$id";
				$otplata = floatval($this->db->getOne($q));
				$dug = $row["iznos_bruto"]-$otplata;
				$dug = $dug>0?' <span style="margin-right:5px; float:right; color:red"><strong>'.number_format($dug,2,",",".").'</strong></span>':"";
				
				// storno
				//$stornoHTML = '<img style="margin:2px 5px;" src="gui/icons/storno.gif" align="right" border="0" alt="storno" />';
				$stornoHTML = "<span style=\"float:right; margin:0px 5px; color:#f00; font-weight:bolder;\">storno</span>";
				$storno = intval($row["tip_ugovora"])==5?" $stornoHTML":"";
				
				$komid = $row["komitent"];
				$komname = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$row["komitent"]." LIMIT 1");
				$komitent = "$komid. $komname";
				$info = datetime2str($row["datum"]);
				$price = "<span style=\"float:right\"><strong>".number_format($row["iznos_bruto"],2,",",".")."</strong></span>";
				$item[] = $price.$dug.$storno."<h1>$komitent</h1><span class=\"info\">$info</span>";
				
				switch ($type){
					case "import":
						$click = "izvodSelectUgovor('$objid','$id','$objbroj','$broj','$objkomid','$komid','$objkomname','".str_replace("'","\\'",$komname)."'); closeYpAjaxForm();";
						break;
					case "uplataAddUgovor":
						$click = "LoadRSDoc('izvodi.php?a=uplataAddUgovor&uplid=$uplid&ugoid=$id&komid=$komid','afterUplataAddUgovor')";
						break;
				}
				$onclick[] = $click;
			}
			$this->assign("list_num",$num);
			$this->assign("list_item",$item);
			$this->assign("onclick",$onclick);
		}
		
		echo $this->parseTemplate("search_list.tpl");
	}
	
	function selectUgovorStrict(){
		$objid = getFromInput("objid");
		$objbroj = getFromInput("objbroj");
		$objkomid = getFromInput("objkomid");
		$objkomname = getFromInput("objkomname");
		
		$k = addslashes(getFromInput("k"));
		
		$q = "SELECT id, broj, datum, komitent, iznos_bruto "
			."FROM ugovori "
			."WHERE broj='$k' LIMIT 1";
		$res = $this->db->select2($q);
		if($res!== false){
			$res = $res[0];
			
			$out = "$objid{!}".$res["id"];
			$out.= "{!}$objbroj{!}".$res["broj"];
			$out.= "{!}$objkomid{!}".$res["komitent"];
			$out.= "{!}$objkomname{!}".$this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$res["komitent"]." LIMIT 1");
		}else{
			$out = "$objid{!}";
			$out.= "{!}$objbroj{!}";
			$out.= "{!}$objkomid{!}";
			$out.= "{!}$objkomname{!}";
		}
		echo $out;
	}
	
	function ugovorDetails(){
		
		$ugoid = getFromInput("id");
		
		list($broj,$iznosb) = $this->db->getRow("SELECT broj, iznos_bruto FROM ugovori WHERE id=$ugoid");
		
		$otplaceno = 0;
		
		$otplata = $this->db->select2("SELECT datum, iznos, izvod FROM ugovori_otplate WHERE ugoid=$ugoid ORDER BY datum");
		if ($otplata !== false) {
			foreach ($otplata as $k=>$v){
				$datum[$k] = int2date($v["datum"]);
				$iznos[$k] = number_format($v["iznos"],2,",",".");
				$izvod[$k] = $v["izvod"];
				$otplaceno += $v["iznos"];
			}
			$this->assign("datum",$datum);
			$this->assign("iznos",$iznos);
			$this->assign("izvod",$izvod);
		}
		
		$dug = $iznosb - $otplaceno;
		if($dug>0){
			$this->assign("nodug"," dug");
		}else{
			$this->assign("nodug"," nodug");
		}
		
		$this->assign("ukupno_otplaceno",number_format($otplaceno,2,",","."));
		$this->assign("broj",$broj);
		$this->assign("ugovor_iznos",number_format($iznosb,2,",","."));
		$this->assign("ugovor_dug",number_format($dug,2,",","."));
		echo $this->parseTemplate("izvodi/ugovor_details.tpl");
	}
	
	//-----------------------------------------------------------
	
	function selectKonto(){
		$k = getFromInput("k");
		$this->assign("search_kword",$k);
		
		$obj = getFromInput("obj");
		
		$res = $this->db->select2("SELECT * FROM kontni_plan WHERE konto LIKE '%$k%' ORDER BY konto");
		if ($res !== false) {
			foreach ($res as $k=>$row){
				$num[]	= $k+1;
				
				$konto = $row["konto"];
				$opis = $row["opis"];
				
				$item[] = "$konto. $opis";
				
				$onclick[] = "izvodSelectKonto('$obj','$konto'); closeYpAjaxForm();";
			}
			$this->assign("list_num",$num);
			$this->assign("list_item",$item);
			$this->assign("onclick",$onclick);
		}
		
		echo $this->parseTemplate("search_list.tpl");
	}
	
	function selectKontoStrict(){
		$k = trim(getFromInput("k"));
		$konto = $this->db->getOne("SELECT konto FROM kontni_plan WHERE konto='$k' LIMIT 1");
		if ($konto) {
			echo "1{!}$konto";
		}else{
			echo "0{!}";
		}
	}
	
	//-----------------------------------------------------------
	
	function selectTipUplate(){
		$k = getFromInput("k");
		$this->assign("search_kword",$k);
		
		$objid = getFromInput("objid");
		$objname = getFromInput("objname");
		
		
		$q = "SELECT * FROM izvodi_tipovi_uplata WHERE name LIKE '%$k%' OR id='$k' ORDER BY name";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$row){
				$id = $row["id"];
				$name = $row["name"];
				$num[] = $id;
				$item[] = $name;
				
				$onclick[] = "izvodSelectTipUplate('$objid','$objname','$id','$name'); closeYpAjaxForm();";
			}
			$this->assign("list_num",$num);
			$this->assign("list_item",$item);
			$this->assign("onclick",$onclick);
		}
		
		echo $this->parseTemplate("search_list.tpl");
	}
	
	function selectTipUplateStrict(){
		$k = getFromInput("k");
		if(preg_match('/^\d+$/',$k)){
			$res = $this->db->getRow("SELECT * FROM izvodi_tipovi_uplata WHERE id=$k LIMIT 1",true);
			if($res){
				echo "0{!}".$res["id"]."{!}".$res["name"];
			}else{
				echo "1{!}";
			}
		}else{
			echo "1{!}";
		}
		
	}
	
	//-----------------------------------------------------------
	
	function save(){
		
		$len = getFromInput("len");
		
		$data = array();
		$getdata = array_merge($_GET,$_POST);
		foreach ($getdata as $k=>$v){
			if (preg_match('/^izvod\d+$/',$k)) {
				$data["izvod"][] = $v;
			}else if (preg_match('/^konto\d+$/',$k)) {
				$data["konto"][] = $v;
			}else if (preg_match('/^ugovorid\d+$/',$k)) {
				$data["ugovor"][] = $v;
			}else if (preg_match('/^komitent_id\d+$/',$k)) {
				$data["komitent"][] = $v;
			}else if (preg_match('/^iznos\d+$/',$k)) {
				$data["iznos"][] = strip_number_format($v);
			}else if (preg_match('/^tipuplateid\d+$/',$k)) {
				$data["tip_uplate"][] = $v;
			}else if (preg_match('/^napomena\d+$/',$k)) {
				$data["napomena"][] = utf8RawUrlDecode(utf8RawChrDecode($v));
			}else if (preg_match('/^izvod_date$/',$k)) {
				$date = date2int($v);
			}
		}
		
		$tmp = array();
		foreach ($data["izvod"] as $k=>$v){
			$tmp[$k] = array(
				"izvod"		=> $v,
				"konto"		=> $data["konto"][$k],
				"ugovor"	=> $data["ugovor"][$k],
				"komitent"	=> $data["komitent"][$k],
				"iznos"		=> $data["iznos"][$k],
				"tip_uplate"=> $data["tip_uplate"][$k],
				"napomena"	=> $data["napomena"][$k],
				"date"		=> $date,
			);
		}
		$data = $tmp;
		
		if (count($data)>0) {
			$code = 0;
			$ok=true;
			$ugoVault = array();
			
			foreach ($data as $k=>$v){
				if (strlen($v["izvod"])>0) {
					if (!$this->checkIt($v)) {
						$ok=false;
						$ugoVault[] = $v;
					}
					$items[] = "(NULL,"
						."'".$v["izvod"]."',"
						."'".$v["konto"]."',"
						."'".$v["ugovor"]."',"
						."'".$v["komitent"]."',"
						."'".$v["iznos"]."',"
						."'".$v["tip_uplate"]."',"
						."'".$v["date"]."',"
						."'".prepForSQL($v["napomena"])."'"
						.",0"
						.")";
				}
			}
			
			if (!$ok) {
				$code = 1;
				$div = "-------------------------------------------------------------------------";
				$message = "Sledeće uplate su već zavedene:\n$div\nizvod\tugovor\tkomitent\n$div";
				foreach ($ugoVault as $obj){
					
					$ugovor = $this->db->getOne("SELECT broj FROM ugovori WHERE id=".$obj["ugovor"]." LIMIT 1");
					$komitent = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$obj["komitent"]." LIMIT 1");
					$message .= "\n".$obj["izvod"]."\t$ugovor\t$komitent";
				}
				$message .= "\n$div\n\nAko želite da izvršite uvoz izvoda,\nprvo izbrišite gore navedene uplate.";
			}else{
				if (count($items)>0) {
					$message = "Uvoz izvršen.";
					$q = "INSERT INTO uplate VALUES".implode(",",$items);
					if (!@$this->db->alterTable($q)) {
						$code = 1;
						$message = "MySQL error: ".mysql_error();
					}
				}else{
					$code = 1;
					$message = "Nothing to import";
				}
			}
		}else {
			$code = 1;
			$message = "Nothing to import. no data";
		}
		
		echo "$code{!}$message";
	}
	
	function checkIt($obj){
		if (!is_array($obj)) {
			return false;
		}
		
		if (((int)$obj["ugovor"])==0) {
			return true;
		}
		
		$ima = $this->db->getOne("SELECT 1 FROM uplate WHERE izvod='".prepForSQL($obj["izvod"])."' AND ugovor_id='".$obj["ugovor"]."' LIMIT 1");
		if ($ima>0) {
			return false;
		}else{
			return true;
		}
	}
	
	//-----------------------------------------------------------
	
	function uplateBrowse(){
		
		$page = (int) getFromInput("page");
		$page = $page>0?$page:1;
		
		$this->assign("current_page",$page);
		
		$filter_date_start = date2int(getFromInput("filter_date_od"));
		$filter_date_end = date2int(getFromInput("filter_date_do"));
		$filter_izvod = readValFromInput("filter_izvod",true);
		$filter_ugovor = readValFromInput("filter_ugovor",true);
		$filter_komitent = readValFromInput("filter_komitent",true);
		$filter_razvedena = intval(getFromInput("filter_razvedena"));
		$filter_tip_uplate = getFromInput("filter_tip_uplate");
		
		$polja = $this->get_IzvodiTipoviUplata($filter_tip_uplate);
			$this->assign("tip_uplate_id",$polja["polje_id"]);
			$this->assign("tip_uplate_name",$polja["polje_ime"]);
			$this->assign("selected_filter_tip_uplate",$polja["polje_selected"]);
			
		$filter_q = "a=uplate";
		$filter_q .= "&filter_date_od=".int2date($filter_date_start);
		$filter_q .= "&filter_date_do=".int2date($filter_date_end);
		$filter_q .= "&filter_izvod=$filter_izvod";
		$filter_q .= "&filter_ugovor=$filter_ugovor";
		$filter_q .= "&filter_komitent=$filter_komitent";
		$filter_q .= "&filter_razvedena=$filter_razvedena";
		$this->assign("filter_query",$filter_q);
		
		$this->assign("filter_date_od",int2date($filter_date_start));
		$this->assign("filter_date_do",int2date($filter_date_end));
		$this->assign("filter_izvod",$filter_izvod);
		$this->assign("filter_ugovor",$filter_ugovor);
		$this->assign("filter_komitent",$filter_komitent);
		switch (intval($filter_razvedena)){
			case 1:
				$this->assign("selected_nerazvedena"," selected");
				break;
			case 2:
				$this->assign("selected_razvedena"," selected");
				break;
		}
		
		
		// FILTER QUERY STRING DATA
		/*$cd = $this->cell_delimiter;
		$rd = $this->row_delimiter;
		$fq  = "page$cd$page$rd";
		$fq .= "filter_date_od$cd$filter_date_start$rd";
		$fq .= "filter_date_do$cd$filter_date_end$rd";
		$fq .= "filter_izvod$cd$filter_izvod$rd";
		$fq .= "filter_ugovor$cd$filter_ugovor$rd";
		$fq .= "filter_komitent$cd$filter_komitent$rd";
		$fq .= "filter_razvedena$cd$filter_razvedena$rd";
		$fq .= "filter_tip_uplate$cd$filter_tip_uplate";
		$this->assign("filter_query",$fq);*/
		
		
		if ($filter_date_start>0 || 
			$filter_date_end>0 || 
			strlen($filter_izvod)>0 || 
			strlen($filter_komitent)>0 || 
			strlen($filter_ugovor)>0 ||
			$filter_razvedena>0) {
		
			$q_select = "SELECT uplate.*";
			$q_from = "FROM uplate";
			$q_where = "WHERE 1";
			
			if ($filter_date_start>0) {
				$q_where .= " AND uplate.datum>=$filter_date_start";
			}
			if ($filter_date_end>0) {
				$q_where .= " AND uplate.datum<=$filter_date_end";
			}
			
			if (strlen($filter_izvod)) {
				$q_where .= " AND uplate.izvod LIKE '%".prepForSQL($filter_izvod)."%'";
			}
			if ($filter_razvedena>0) {
				$q_where .= " AND uplate.linked=".($filter_razvedena-1);
			}
			
			if (strlen($filter_ugovor)) {
				$q_from	 .= ", ugovori";
				$q_where .= " AND ugovori.id=uplate.ugovor_id";
				$q_where .= " AND (ugovor_id=".intval($filter_ugovor)." OR ugovori.broj LIKE '%".prepForSQL($filter_ugovor)."%')";
			}
			if (strlen($filter_komitent)) {
				$q_from  .= ", komitenti";
				$q_where .= " AND uplate.komitent=komitenti.komitent_id";
				$q_where .= " AND (uplate.komitent=".intval($filter_komitent)." OR komitenti.komitent_name LIKE '%".prepForSQL($filter_komitent)."%' OR komitenti.komitent_name1 LIKE '%".prepForSQL($filter_komitent)."%')";
			}
			if ($filter_tip_uplate>0) {
				$q_where .= " AND tip_uplate=$filter_tip_uplate";
			}
			
			$ukupno = $this->db->getOne("SELECT count(*) $q_from $q_where");
			$this->assign("ukupno",$ukupno);
			
			$numpages = bcdiv($ukupno,PAGE_LENGTH,0);
			$numpages = $ukupno%PAGE_LENGTH>0?$numpages+1:$numpages;
			for ($i=0; $i<$numpages; $i++){
				$pages[$i] = $i+1;
				$pages_curr[$i] = $page==($i+1)?" id=\"current\"":"";
			}
			$this->assign("page",$pages);
			$this->assign("page_curr",$pages_curr);
			
			$start = ($page-1)*PAGE_LENGTH;
			$q = "$q_select $q_from $q_where ORDER BY uplate.datum DESC LIMIT $start,".PAGE_LENGTH;
			
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $k=>$row){
					$uplataid[] = $row["uplata_id"];
					$izvod[] = $row["izvod"];
					$konto[] = $row["konto"];
					$ugoid[] = $row["ugovor_id"]>0?$row["ugovor_id"]:"dodeli uplatu ugovoru";
					$imaugovor[] = intval($row["ugovor_id"]>0)?"1":"0";
					$ugobroj[] = $row["ugovor_id"]>0?$this->db->getOne("SELECT broj FROM ugovori WHERE id=".$row["ugovor_id"]." LIMIT 1"):"<img src=\"gui/icons/ugovor.gif\" border=\"0\" />";
					$komid[] = $row["komitent"];
					$komname[] = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=".$row["komitent"]." LIMIT 1");
					$iznos[] = number_format($row["iznos"],2,",",".");
					$tipuplate[] = $this->db->getOne("SELECT name FROM izvodi_tipovi_uplata WHERE id=".$row["tip_uplate"]." LIMIT 1");
					$datum[] = int2date($row["datum"]);
					$napomena[] = nl2br($row["napomena"]);
					
					$razz1 = $row["linked"]>0?true:false;
					$q = "SELECT 1 FROM ugovori_otplate "
						."WHERE ugoid=".$row["ugovor_id"]." "
						."AND izvod='".$row["izvod"]."' "
						."AND datum=".$row["datum"];
					$razz2 = $row["ugovor_id"]>0?$this->db->getOne($q):0;
					$razz2 = $razz2>0?true:false;
					
					
					$razvedena[] = $razz1 && $razz2?"DA":"NE";
					
					$razvedena_int[] = $razz1 && $razz2 ?"1":"0";
				}
				$this->assign("uplataid",$uplataid);
				$this->assign("izvod",$izvod);
				$this->assign("konto",$konto);
				$this->assign("ugoid",$ugoid);
				$this->assign("imaugovor",$imaugovor);
				$this->assign("ugobroj",$ugobroj);
				$this->assign("komitent_id",$komid);
				$this->assign("komitent",$komname);
				$this->assign("iznos",$iznos);
				$this->assign("tipuplate",$tipuplate);
				$this->assign("datum",$datum);
				$this->assign("napomena",$napomena);
				$this->assign("razvedena",$razvedena);
				$this->assign("razvedena_int",$razvedena_int);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema rezultata.");
			}
		}
		
		echo $this->parseTemplate("izvodi/uplate_browse.tpl");
	}
	
	function linkUplata(){
		$code = 0;
		$message = "Uplata/e su uspešno razvedene.";
		
		$id = explode(";",getFromInput("id"));
		if (count($id)<1) {
			die();
		}
		foreach ($id as $uplataid){
			
			$uplata = $this->db->getRow("SELECT * FROM uplate WHERE uplata_id=$uplataid LIMIT 1",true);
			
			if ($uplata["ugovor_id"]>0) {
				
				$sql = "SELECT porez_percent FROM porezi, ugovori WHERE ugovori.porez=porezi.porez_id AND ugovori.id=".$uplata["ugovor_id"]." LIMIT 1";
				$porez_perc = $this->db->getOne($sql);
				$bruto = $uplata["iznos"];
				$neto = round($bruto/((100+$porez_perc)/100),2);
				$porez = $bruto-$neto;
				
				$q = "INSERT INTO ugovori_otplate VALUES("
					.$uplata["ugovor_id"].","
					.$uplata["datum"].","
					."'$bruto',"
					."'$neto',"
					."'$porez',"
					."'".$uplata["izvod"]."'"
					.",''"
					.")";
				if (!@$this->db->alterTable($q)) {
					$code = 1;
					$message = "Uplata za ugovor ".$uplata["ugovor_id"].", $bruto din. nije razvedena!\n\n".mysql_error();
					break;
				}
				
				$q = "UPDATE uplate SET linked=1, tip_uplate=0 WHERE uplata_id=$uplataid LIMIT 1";
				if(!@$this->db->alterTable($q)){
					$code = 1;
					$message = "Update same uplate nije uspeo.".mysql_error();
					break;
				}
			}
		}
		
		echo "$code{!}$message";
	}
	
	function uplataAddUgovor(){
		$uplid = getFromInput("uplid");
		$ugoid = getFromInput("ugoid");
		$komid = getFromInput("komid");
		
		$code = 0;
		$message = "Uplata uspešno povezana.";
		
		$izvod = $this->db->getOne("SELECT izvod FROM uplate WHERE uplata_id=$uplid LIMIT 1");
		
		$vecpovezano = (int) $this->db->getOne("SELECT 1 FROM uplate WHERE izvod='$izvod' AND ugovor_id=$ugoid LIMIT 1");
		
		if ($vecpovezano) {
			$code = 1;
			$message = "Greška!\nUplata izvod:$izvod, ID ugovora:$ugoid je već evidentirana.";
		}else{
			$q = "UPDATE uplate SET ugovor_id=$ugoid, komitent=$komid WHERE uplata_id=$uplid LIMIT 1";
			if (!$this->db->alterTable($q)) {
				$code = 1;
				$message = "Greška!\n".mysql_error();
			}
		}
		
		echo "$code{!}$message";
	}
	
	function uplataDelete(){
		$uplata = getFromInput("id");
		
		$code = 0;
		$message = "Uplata je obrisana.";
		
		$q = "DELETE FROM uplate WHERE uplata_id=$uplata LIMIT 1";
		if (!$this->db->alterTable($q)) {
			$code = 1;
			$message = "Error!\n".mysql_error();
		}
		
		echo "$code{!}$message";
	}
	
	function uplataEdit(){
		$uplid = getFromInput("id");
		$fq = readValFromInput("fq");
		
		$this->assign("uplata_id",$uplid);
		$this->assign("filter_query",$fq);
		
		$uplata = $this->db->getRow("SELECT * FROM uplate WHERE uplata_id=$uplid",true);
		
		$polja = $this->get_KontniPlan($uplata["konto"]);
			$this->assign("konto",$polja["polje_id"]);
			$this->assign("konto_opis",$polja["polje_ime"]);
			$this->assign("konto_selected",$polja["polje_selected"]);
		
		echo $this->parseTemplate("izvodi/uplata_edit.tpl");
	}
	
	function uplataEditSave(){
		$fq = readValFromInput("filter_query");
		
		echo $fq;
	}
	
	//-----------------------------------------------------------
	
	function get_IzvodiTipoviUplata($tipid=null){
		$ret = array();
		$q = "SELECT * FROM izvodi_tipovi_uplata ORDER BY name ASC";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["id"];
				$ret["polje_ime"][$k] = $v["name"];
				$ret["polje_selected"][$k] = $v["id"]==$tipid?" selected":"";
			}
		}
		return $ret;
	}
	
	function get_KontniPlan($konto=null){
		$q = "SELECT * FROM kontni_plan ORDER BY konto";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $k=>$v){
				$ret["polje_id"][$k] = $v["konto"];
				$ret["polje_ime"][$k] = $v["opis"];
				$ret["polje_tip"][$k] = $v["dug_potr"];
				$ret["polje_selected"][$k] = $v["konto"]==$konto?" selected":"";
			}
		}
		return $ret;
	}
}

?>